package Controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import Entity.Product;
import Communication.Client;
import Communication.MessageHandle;
import Entity.Type;
import Entity.Category;
import Entity.Worker;
import Controller.NewProductToCatalogController.NewProductToCatalogListener;
import View.LoginView;
import View.NewProductToCatalogView;
import DB.ProductQuery;

/** This class controls and manages the process that insert New product to company catalog */
public class NewProductToCatalogController implements Serializable {
	private static final long serialVersionUID = 42L;

	/**  Required attributes */
	NewProductToCatalogView ProductView;
	Client client;
	ProductQuery productModel;
	JFileChooser chooser=null;
	FileNameExtensionFilter filter;
	static ArrayList<Product> productsList=new ArrayList<Product>();
	static Product Prd;

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param workerMainMenuController
	 */
	public NewProductToCatalogController (Client client, WorkerMainMenuController workerMainMenuController) {
		this.client = client;
		ProductView = new NewProductToCatalogView();
		ProductView.showWindow();
		ProductView.addNewProductToCatalogListener(new NewProductToCatalogListener(workerMainMenuController));
		ProductView.addupload(new uploadListener());
	}


	/**  Inner Listener class */ 
	class NewProductToCatalogListener implements ActionListener
	{
		WorkerMainMenuController workerMainMenuController;

		/**
		 * creats NewProductToCatalog Listener
		 * @param workerMainMenuController
		 */
		public  NewProductToCatalogListener(WorkerMainMenuController workerMainMenuController){
			this.workerMainMenuController = workerMainMenuController;
			productModel = new ProductQuery(client);
		}


		/**  
		 * Handle Actions
		 */
		public void actionPerformed(ActionEvent event) {


			if (event.getSource() == ProductView.getSubmitButton()){

				if(productModel.isEmpty(ProductView.getProductID()) || productModel.isEmpty(ProductView.getProductName()) 
						|| productModel.isEmpty(ProductView.getProductDescription()) || (chooser.getSelectedFile()==null))
					SystemController.displayErrorMessage("One or more fields are Empty");

				else if(ProductView.getProductPrice()==0)
					SystemController.displayErrorMessage("Price Field should contain Number");

				else 
				{   /** create new instance of product entity and set all attributes from user */

					Prd=new Product();
					Prd.setProductID(ProductView.getProductID());
					Prd.setProductName(ProductView.getProductName());
					Prd.setPrice(ProductView.getProductPrice());
					Prd.setPicture(chooser.getSelectedFile().getName());
					Prd.setDescription(ProductView.getProductDescription());
					System.out.print(Prd.getProductID());

					try {

						/**  send to server according to complete the process */
						MessageHandle msg = new MessageHandle(Prd, MessageHandle.PRODUCT, false);
						client.sendToServer(msg);

					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace( );
					}

					ProductView.showWindow();
					workerMainMenuController.hideWindow();
					ProductView.clearFields();
				}
			}
			else if (event.getSource() == ProductView.getBackBtn())
			{
				workerMainMenuController.showWindow();
				ProductView.hideWindow();
				ProductView.clearFields();
			}
		}
	}

	/**
	 * handles the message received from server - if the DB was updated or not and why.
	 * @param msg
	 */
	public static void updataView(String msg){
		if (msg.compareTo("new")==0){
			SystemController.displayMessage("New Product successfully added");
			productsList.add(Prd);
		}
		else SystemController.displayErrorMessage("The Product already exists");
	}

	/** Inner class to manage the upload listener to upload the product picture */
	public class  uploadListener  implements ActionListener

	{	
		public uploadListener()
		{

		}
		/**
		 * Handle Actions
		 */
		public void actionPerformed(ActionEvent event) {


			if(event.getSource()==NewProductToCatalogView.getUploadBtn()  )
			{
				chooser = new JFileChooser();
				filter = new FileNameExtensionFilter(
						"JPG & GIF Images", "jpg", "gif");
				chooser.setFileFilter(filter);
				int returnVal = chooser.showOpenDialog(NewProductToCatalogView.getUploadBtn());
				if(returnVal == JFileChooser.APPROVE_OPTION) {

				}
			}
		}
	}
}

